package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.MemberQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("member")
public class MemberController {

    @Autowired
    private IMemberService service;
    //页面跳转
    @RequestMapping("")
    public String index(){
        return "member/memberList";
    }


    //分页查询
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(MemberQueryObject qo){
        PageResult pageResult = service.queryPage(qo);
        return pageResult;
    }
    //保存
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Member entity){
        JsonResult jr = new JsonResult();
        try {
            service.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }
    //修改全部
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Member entity){
        JsonResult jr = new JsonResult();
        try {
            service.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }

    //修改状态
    @RequestMapping("updateState")
    @ResponseBody
    public JsonResult updateState(Member entity){
        JsonResult jr = new JsonResult();
        try {
            service.updateState(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }
    //修改标签
    @RequestMapping("updateLabel")
    @ResponseBody
    public JsonResult updateLabel(Member entity){
        JsonResult jr = new JsonResult();
        try {
            service.updateLabel(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }
    //修改分组
    @RequestMapping("updateGroup")
    @ResponseBody
    public JsonResult updateGroup(Member entity){
        JsonResult jr = new JsonResult();
        try {
            service.updateGroup(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }
    //memberlist用于message信息做查询会员使用
    @ResponseBody
    @RequestMapping("memberlist")
    public List<Member> memberlist(){
        return service.querymemberlist();
    }


    //李超凡礼品兑换积分属性值减少update方法
    //修改状态
    @RequestMapping("integralUpdate")
    @ResponseBody
    public JsonResult integralUpdate(Long id,Long number){
        JsonResult jr = new JsonResult();
        try {
            service.updateIntegral(id,number);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setMsg(e.getMessage());
        }
        return jr;
    }
    //会员余额的修改
    @RequestMapping("memberBalanceUpdate")
    @ResponseBody
    public JsonResult memberBalanceUpdate(Long id,BigDecimal salePrice){
        JsonResult result = new JsonResult();
        try {
            service.memberBalanceUpdate(id,salePrice);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
